package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.BusinessStaffShenpiDao;
import com.pojo.BusinessStaffShenpi;
import com.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商户员工审批业务处理类
 *
 * @author tangbo
 */
@Service
public class BusinessStaffShenpiService extends ServiceImpl<BusinessStaffShenpiDao, BusinessStaffShenpi> {
    @Autowired
    private BusinessStaffShenpiDao businessStaffShenpiDao;

    /**
     * 获取商户员工已审批或未审批记录
     * @param name 关键字：员工姓名
     * @param businessId 商户ID
     * @param staffStatus 员工申请状态：0离职、1入职
     * @param status 审批状态：0审批中、1审批完成
     * @param result 审批结果：0驳回、1通过
     * @param page
     * @param limit
     * @return
     */
    public IPage<BusinessStaffShenpi> getBusinessStaffSp (String name, String businessId, String staffStatus, String status, String result, Integer page, Integer limit) {
        IPage p = new Page(page, limit);
        QueryWrapper query = new QueryWrapper();
        if (ValidateHelper.isNotEmptyString(name)) {
            query.like("staffName", name);
        }
        if (ValidateHelper.isNotEmptyString(businessId)) {
            query.eq("businessId", businessId);
        }
        if (ValidateHelper.isNotEmptyString(staffStatus)) {
            query.eq("staffStatus", staffStatus);
        }
        if (ValidateHelper.isNotEmptyString(status)) {
            query.eq("status", status);
        }
        if (ValidateHelper.isNotEmptyString(result)) {
            query.eq("result", result);
        }
        query.orderByDesc("createTime");
        IPage iPage = businessStaffShenpiDao.selectPage(p, query);
        return iPage;
    }

    /**
     * 根据商户ID查询该商户下未审批的商户员工
     * @param businessId
     * @return
     */
    public IPage<BusinessStaffShenpi> getBusinessStaffWspByBusinessId (String businessId, Integer page, Integer limit) {
        IPage p = new Page(page,limit);
        QueryWrapper query = new QueryWrapper();
        query.eq("businessId", businessId);
        query.eq("status", 0);
        return businessStaffShenpiDao.selectPage(p, query);
    }

    /**
     * 根据员工编号和审批类型查询员工审批记录
     * @param staffBh 员工编号
     * @param staffStatus 员工申请类型：0离职、1入职
     * @return
     */
    public BusinessStaffShenpi getBusinessStaffSpByBh (String staffBh,Integer staffStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("staffBh", staffBh);
        queryWrapper.eq("staffStatus", staffStatus);
        queryWrapper.eq("status", 0);
        queryWrapper.last("limit 1");
        return businessStaffShenpiDao.selectOne(queryWrapper);
    }
    /**
     * 根据员工证件号码和审批类型查询员工审批记录
     * @param staffSfz 员工证件号码
     * @param staffStatus 员工申请类型：0离职、1入职
     * @return
     */
    public BusinessStaffShenpi getBusinessStaffSpBySfz (String staffSfz,Integer staffStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("staffSfz", staffSfz);
        queryWrapper.eq("staffStatus", staffStatus);
        queryWrapper.eq("status", 0);
        queryWrapper.last("limit 1");
        return businessStaffShenpiDao.selectOne(queryWrapper);
    }
}
